package com.ifmvo.togetherad.core.helper

import android.app.Activity
import android.content.Context
import android.view.ViewGroup
import com.ifmvo.togetherad.core.R
import com.ifmvo.togetherad.core.TogetherAd
import com.ifmvo.togetherad.core.config.AdProviderLoader
import com.ifmvo.togetherad.core.life.ActivityLifeManager
import com.ifmvo.togetherad.core.listener.BannerListener
import com.ifmvo.togetherad.core.provider.BaseAdProvider
import com.ifmvo.togetherad.core.utils.DispatchUtil
import com.ifmvo.togetherad.core.utils.loge
import org.jetbrains.annotations.NotNull
import java.lang.ref.WeakReference

/**
 * Banner 横幅广告
 *
 * Created by Matthew Chen on 2020/5/25.
 */
object AdHelperBanner : BaseHelper() {

    private var adProvider: BaseAdProvider? = null
    private var activityAdsHolder: ActivityLifeManager.ActivityAdsHolder? = null

    //为了照顾 Java 调用的同学
    fun show(
        context: Context?,
        @NotNull alias: String,
        @NotNull container: ViewGroup,
        listener: BannerListener? = null
    ) {
        show(context, alias, null, container, listener)
    }

    fun show(
        context: Context?,
        @NotNull alias: String,
        ratioMap: LinkedHashMap<String, Int>? = null,
        @NotNull container: ViewGroup,
        listener: BannerListener? = null
    ) {
        startTimer(listener)
        realShow(context, alias, ratioMap, container, listener)
    }

    private fun realShow(
        context: Context?,
        @NotNull alias: String,
        ratioMap: LinkedHashMap<String, Int>? = null,
        @NotNull container: ViewGroup,
        listener: BannerListener? = null
    ) {

        val currentRatioMap =
            if (ratioMap?.isEmpty() != false) TogetherAd.getPublicProviderRatio() else ratioMap

        val adProviderType = DispatchUtil.getAdProvider(alias, currentRatioMap)

        if (adProviderType?.isEmpty() != false) {
            cancelTimer()
            listener?.onAdFailedAll(FailedAllMsg.failedAll_noDispatch)
            return
        }

        adProvider = AdProviderLoader.loadAdProvider(adProviderType)

        if (adProvider == null) {
            "$adProviderType ${context?.resources?.getString(R.string.no_init)}".loge()
            val newRatioMap = filterType(currentRatioMap, adProviderType)
            realShow(context, alias, newRatioMap, container, listener)
            return
        }
        var activity: Activity? = ActivityLifeManager.getActivity(context)
        if (activity != null) {
            activityAdsHolder = ActivityLifeManager.ActivityAdsHolder(
                WeakReference(activity),
                WeakReference(this)
            )
            ActivityLifeManager.requireDestroyAdsList.add(activityAdsHolder!!)
        }
        adProvider?.showBannerAd(
            context = context,
            adProviderType = adProviderType,
            alias = alias,
            container = container,
            listener = object : BannerListener {
                override fun onAdStartRequest(providerType: String) {
                    listener?.onAdStartRequest(adProviderType)
                }

                override fun onAdLoaded(providerType: String) {
                    if (isFetchOverTime) return

                    cancelTimer()
                    listener?.onAdLoaded(providerType)
                }

                override fun onAdFailed(providerType: String, failedMsg: String?) {
                    if (isFetchOverTime) return

                    val newRatioMap = filterType(currentRatioMap, adProviderType)
                    realShow(activity, alias, newRatioMap, container, listener)

                    listener?.onAdFailed(providerType, failedMsg)
                    activityAdsHolder?.remove = true
                    adProvider = null
                }

                override fun onAdClicked(providerType: String) {
                    listener?.onAdClicked(providerType)
                }

                override fun onAdExpose(providerType: String) {
                    listener?.onAdExpose(providerType)
                    adProvider = null
                }

                override fun onAdClose(providerType: String) {
                    listener?.onAdClose(providerType)
                    activityAdsHolder?.remove = true
                    adProvider = null
                }
            })
    }

    override fun destroy() {
        adProvider?.destroyBannerAd()
        adProvider = null
        activityAdsHolder?.remove = true
        activityAdsHolder = null

    }
}